package com.ys.controller.web.finance;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.finance.TcwCapitalClaim;
import com.ys.entity.finance.TcwCapitalClaimSon;
import com.ys.service.finance.TcwCapitalClaimService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.Flow;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资金认领单
 */
@Controller
@RequestMapping("/web/finance/tcwCapitalClaim")
public class WebTcwCapitalClaimController {
	private static final Logger logger = LoggerFactory.getLogger(WebTcwCapitalClaimController.class);

	@Autowired
	private CommonApiService<TcwCapitalClaim> commonApiService;

    @Autowired
    private YspTaskService yspTaskService;

    @Autowired
    private TcwCapitalClaimService tcwCapitalClaimService;

    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            TcwCapitalClaim entity = new TcwCapitalClaim();
            //关联数据
            String bid = commonApiService.getCommonNextBid(MenuId.TCW_CapitalClaim);
            entity.setBid(bid);
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_CapitalClaim, new ArrayList<Map<String, Object>>());
            return commonApiService.toAdd(model, entity, MenuId.TCW_CapitalClaim, null, subGridConfig);
        }catch (Exception e) {
            logger.error("新增页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
        try {
            //model.addAttribute("onProcess", 0);
            Map<String,Object> extraParam = tcwCapitalClaimService.getExtraParam(bid);

            List<Map<String, Object>> itemList = tcwCapitalClaimService.getItemList(bid);

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_CapitalClaim,itemList);
            //子表按钮
            Integer state = yspTaskService.flowState(bid);
            if (state == Flow.State.FINISH.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }else if (state == Flow.State.PROCESS.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }
            if(extraParam.get("instBid")!=null && !"".equals(extraParam.get("instBid"))){
                return commonApiService.toEdit(model, Long.parseLong(extraParam.get("id").toString()), new TcwCapitalClaim(), MenuId.TCW_CapitalClaim, extraParam, subGridConfig);
            }else {
                return commonApiService.toDetail(model, Long.parseLong(extraParam.get("id").toString()), new TcwCapitalClaim(), MenuId.TCW_CapitalClaim, extraParam, subGridConfig);
            }

        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }









}






